package org.parademo.demo;

import jess.JessException;
import org.parademo.database.DataStorageImpl;
import org.parademo.model.Category;
import org.parademo.model.ProposalEntity;
import org.parademo.model.ProposalStatus;
import org.parademo.model.VoteEntity;
import org.parademo.reasoning.ReasoningEngine;

import java.util.GregorianCalendar;

/**
 * @author Ajit Skanda
 * @version 1.0
 */
public class DemoExecutor {

	public static void main(String[] args) {
		try {
			DataStorageImpl dataStorage = new DataStorageImpl();
			ReasoningEngine reasoningEngine = new ReasoningEngine(dataStorage);

            GregorianCalendar startCalendar = new GregorianCalendar(2011, 7, 1, 0 , 0);
            GregorianCalendar endCalendar = new GregorianCalendar(2011, 8, 1, 0 , 0);

			// An 'Internal' proposal to test rule-02.
			ProposalEntity proposal = new ProposalEntity("test proposal 1", "test proposal 1", startCalendar.getTime(), endCalendar.getTime(), Category.Environment, dataStorage.getAllUsers().get("user0"), ProposalStatus.CREATED);
			processProposal(reasoningEngine, proposal);

            // A proposal with votes to test rule-03.
            proposal = new ProposalEntity("75% percentage test-1", "75% percentage test-1", startCalendar.getTime(), endCalendar.getTime(), Category.Internal, dataStorage.getAllUsers().get("user1"), ProposalStatus.CREATED);
            for (int i=0; i< 10; i++) {
                Long id = dataStorage.getAllUsers().get("user" + i).getId();
                proposal.getVotes().add(new VoteEntity(id, proposal.getId(), 5, true));
            }
			//System.out.println("proposal.getAuthor().getCommunity() = " + proposal.getAuthor().getCommunity().getMembers().size());
            processProposal(reasoningEngine, proposal);
			
			//delete a given rule
			deleteRule(reasoningEngine, "rule0A-to-undef",  "(assert (triggerUndefRule (one 10)) )");

		} catch (JessException e) {
			e.printStackTrace();
		}
	}

	private static void processProposal(ReasoningEngine reasoningEngine, ProposalEntity proposal) throws JessException {
		reasoningEngine.run(proposal);
	}

	private static void deleteRule(ReasoningEngine reasoningEngine, String ruleName, String assertStatement) throws JessException {
		reasoningEngine.deleteRule(ruleName, assertStatement);
	}
}
